﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Win32Import;

namespace FennecUI
{
    public class NativeApplet : Applet
    {
        public NativeApplet(AppletConfiguration appletConfig)
            : base(appletConfig)
        {

        }

        //  Override size so that the window get resized with the control;
        new public System.Drawing.Size Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
                Win32.MoveWindow(_hWnd, 0, 0, Width, Height - 40, true);
            }
        }

        new public int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
                Win32.MoveWindow(_hWnd, 0, 0, Width, Height - 40, true);
            }
        }

        new public int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
                Win32.MoveWindow(_hWnd, 0, 0, Width, Height - 40, true);
            }
        }


        protected override void OnClose(Applet sender, EventArgs e)
        {
            if(!_process.HasExited) // In case user has manually killed the process
                _process.Kill();
            _process.Close();
        }

        protected override void onEnterFullMode()
        {
            Win32.MoveWindow(_hWnd, 0, 0, Width, Height - 40, true);
        }

        protected override void onLeaveFullMode()
        {
            Win32.MoveWindow(_hWnd, 0, 0, Width, Height - 40, true);
        }

        private Process _process;
        private IntPtr _hWnd;

        /// <summary>
        /// Start the application and capture the window
        /// FileName must be set before calling this fuction
        /// </summary>
        public override void StartApplet()
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = Configuration.FilePath;
            psi.Arguments = Configuration.StartParameters;
            psi.EnvironmentVariables.Add("APPLET_DIR", Environment.ExpandEnvironmentVariables(@"%APPLETS_STORE%\"+Configuration.Name));
            psi.UseShellExecute = false;
            _process = new Process();
            _process.StartInfo = psi;
            _process.Start();

            _process.WaitForInputIdle();    //  Wait for the child window to load

            _hWnd = _process.MainWindowHandle;  //Get handle to main window

            if (_hWnd == IntPtr.Zero)   // If handle is not set, find it manually
            {
                enumWindowsCallback = new Win32.EnumWindowsProc(findWindow);
                Win32.EnumWindows(enumWindowsCallback, _process.Id); // Loop over all top level windows until the windows process id matches this process
            }

            Win32.SetParent(_hWnd, AppletContent.Handle); // Set parent of hwnd to this control

            Win32.SetWindowLong(_hWnd, GWL.STYLE, (uint)(WS.POPUP | WS.VISIBLE | WS.CLIPSIBLINGS)); // Set window style, removes captions from window
            Win32.SetWindowLong(_hWnd, GWL.EXSTYLE, (uint)WS_EX.APPWINDOW);

            Win32.MoveWindow(_hWnd, 0, 0, Width, Height - 40, true); //Size and position window
        }

        private Win32.EnumWindowsProc enumWindowsCallback;


        private bool findWindow(IntPtr hWnd, int lParam)
        {
            IntPtr processId = IntPtr.Zero;
            Win32.GetWindowThreadProcessId(hWnd, out processId);
            if (lParam == processId.ToInt32())
            {
                this._hWnd = hWnd;
                return false;
            }
            return true;
        }
    }
}
